package ynu.edu.model.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import ynu.edu.common.domain.PageDTO;
import ynu.edu.model.domain.HanziGenerationResponse;
import ynu.edu.model.entity.HanziGeneration;
import ynu.edu.model.mapper.HanziGenerationMapper;
import ynu.edu.model.service.HanziGenerationService;


import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 汉字生成记录Service实现类
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class HanziGenerationServiceImpl extends ServiceImpl<HanziGenerationMapper, HanziGeneration> implements HanziGenerationService {


    @Override
    public boolean updateStatus(Long id, String status, String ossUrl, Long generationTime, String errorMessage) {
        HanziGeneration entity = getById(id);
        if (entity == null) {
            return false;
        }
        
        entity.setStatus(status);
        entity.setOssUrl(ossUrl);
        entity.setGenerationTime(generationTime);
        entity.setErrorMessage(errorMessage);
        entity.setUpdateTime(LocalDateTime.now());
        
        return updateById(entity);
    }

    @Override
    public List<HanziGenerationResponse> findByChineseChar(String chineseChar) {
        LambdaQueryWrapper<HanziGeneration> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(HanziGeneration::getChineseChar, chineseChar)
               .orderByDesc(HanziGeneration::getCreateTime);
        
        List<HanziGeneration> entities = list(wrapper);
        return entities.stream()
                .map(HanziGenerationResponse::fromEntity)
                .collect(Collectors.toList());
    }

    @Override
    public List<HanziGenerationResponse> findByStatus(String status) {
        LambdaQueryWrapper<HanziGeneration> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(HanziGeneration::getStatus, status)
               .orderByDesc(HanziGeneration::getCreateTime);
        
        List<HanziGeneration> entities = list(wrapper);
        return entities.stream()
                .map(HanziGenerationResponse::fromEntity)
                .collect(Collectors.toList());
    }

    @Override
    public HanziGenerationResponse findByRequestId(String requestId) {
        LambdaQueryWrapper<HanziGeneration> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(HanziGeneration::getRequestId, requestId);
        
        HanziGeneration entity = getOne(wrapper);
        return HanziGenerationResponse.fromEntity(entity);
    }

    @Override
    public PageDTO<HanziGeneration> getPage(int page, int size) {
        Page<HanziGeneration> pageParam = new Page<>(page, size);
        LambdaQueryWrapper<HanziGeneration> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(HanziGeneration::getCreateTime);
        
        Page<HanziGeneration> result = page(pageParam, wrapper);
        return PageDTO.of(result, HanziGeneration.class);
    }

    @Override
    public long countRecords() {
        return count();
    }

    @Override
    public long countSuccessRecords() {
        LambdaQueryWrapper<HanziGeneration> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(HanziGeneration::getStatus, HanziGeneration.Status.SUCCESS.getCode());
        return count(wrapper);
    }

    @Override
    public long countFailedRecords() {
        LambdaQueryWrapper<HanziGeneration> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(HanziGeneration::getStatus, HanziGeneration.Status.FAILED.getCode());
        return count(wrapper);
    }

    @Override
    public long countProcessingRecords() {
        LambdaQueryWrapper<HanziGeneration> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(HanziGeneration::getStatus, HanziGeneration.Status.PROCESSING.getCode());
        return count(wrapper);
    }
} 